package java_thinking.unit_14._14_4;

/**
 * @Description :   工厂设计模式
 *                  Pet继承结构中存在一个问题，即每次向该几成结构添加新的Pet类型时，
 *                  必须将其添加为LiteralPetCreator.java中
 *                  在下面的例子中基类Part包含一个工厂对象列表，油createRandom()方法产生的类型
 *                  ，他们的工厂都被添加到了partFactories List中，从而被注册到基类中
 * @author: cww
 * @DateTime: 2019-03-30 10:08
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author asd730520@live.com
 */
public class PartFactory {
    @Override
    public String toString() {
        return getClass().getSimpleName();
    }

    private static List<Factory<? extends PartFactory>> partFactories = new ArrayList<>();

    static {
        partFactories.add(new FuelFilter.Factory());
        partFactories.add(new AirFilter.Factory());
        partFactories.add(new CabinAirFilter.Factory());
        partFactories.add(new OilFilter.Factory());
        partFactories.add(new FanBelt.Factory());
        partFactories.add(new GeneratorBelt.Factory());
        partFactories.add(new PowerSteeringBelt.Factory());
        //这里如果用Collections.addAll()来向列表中添加工厂会提示编译出错，“创建泛型数组”不被允许
    }

    private static Random rand = new Random(47);

    public static PartFactory createRandom() {
        int n = rand.nextInt(partFactories.size());
        return partFactories.get(n).create();
    }
}
class Filter extends PartFactory{}

class FuelFilter extends Filter {
    public static class Factory implements java_thinking.unit_14._14_4.Factory<FuelFilter> {
        @Override
        public FuelFilter create(){
            return new FuelFilter();
        }
    }
}
class AirFilter extends Filter {
    public static class Factory implements java_thinking.unit_14._14_4.Factory<AirFilter> {
        @Override
        public AirFilter create(){
            return new AirFilter();
        }
    }
}
class CabinAirFilter extends Filter {
    public static class Factory implements java_thinking.unit_14._14_4.Factory<CabinAirFilter> {
        @Override
        public CabinAirFilter create(){
            return new CabinAirFilter();
        }
    }
}
class OilFilter extends Filter {
    public static class Factory implements java_thinking.unit_14._14_4.Factory<OilFilter> {
        @Override
        public OilFilter create(){
            return new OilFilter();
        }
    }
}
class Belt extends PartFactory {}

class FanBelt extends Belt {
    public static class Factory implements java_thinking.unit_14._14_4.Factory<FanBelt> {
        @Override
        public FanBelt create(){
            return new FanBelt();
        }
    }
}
class GeneratorBelt extends Belt {
    public static class Factory implements java_thinking.unit_14._14_4.Factory<GeneratorBelt> {
        @Override
        public GeneratorBelt create(){
            return new GeneratorBelt();
        }
    }
}
class PowerSteeringBelt extends Belt {
    public static class Factory implements java_thinking.unit_14._14_4.Factory<PowerSteeringBelt> {
        @Override
        public PowerSteeringBelt create(){
            return new PowerSteeringBelt();
        }
    }
}
class RegisteredFactories {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++){
            System.out.println(PartFactory.createRandom());
        }
    }
}


